package com.code2roc.waragent;

import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.apache.tomcat.util.http.CookieProcessor;
import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.apache.tomcat.util.scan.StandardJarScanFilter;
import org.apache.tomcat.util.scan.StandardJarScanner;

import java.io.File;

public class Application {
    public static void main(String[] args) {
        try {
            String[] paramList = args[0].split("★");
            String name = paramList[0].trim();
            String catalinaBase = paramList[1].trim();
            int port = Integer.parseInt(paramList[2].trim());
            String contextPath = paramList[3].trim();
            String jvmStartCommand = String.valueOf(paramList[4]).trim();
            System.out.println("【name】：" + name);
            System.out.println("【catalinaBase】：" + catalinaBase);
            System.out.println("【port】：" + port);
            System.out.println("【contextPath】：" + contextPath);
            System.out.println("【jvmStartCommand】：" + jvmStartCommand);
            String skipScan = "";
            if (paramList.length == 6) {
                skipScan = paramList[5].trim();
            }
            System.out.println("【skipScan】：" + skipScan);
            start(name, catalinaBase, port, contextPath, jvmStartCommand, skipScan);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void start(String name, String catalinaBase, int port, String contextPath, String jvmStartCommand, String skipScan) throws Exception {
        String externalConfigDir = "file:" + catalinaBase + File.separator + "webapps" + File.separator;
        System.setProperty("spring.config.additional-location", externalConfigDir);
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir(catalinaBase);
        tomcat.getHost().setAutoDeploy(false);
        tomcat.getHost().setAppBase(catalinaBase + File.separator + "webapps");
        tomcat.getServer().setCatalinaHome(new File(catalinaBase));
        tomcat.getServer().setCatalinaBase(new File(catalinaBase));
        //设置Tomcat端口
        tomcat.setPort(port);
        // 设置webapp资源路径
        StandardContext ctx = (StandardContext) tomcat.addWebapp(contextPath, catalinaBase + File.separator + "webapps" + File.separator + name + ".war");
        // 设置上下文路每径
        ctx.setReloadable(false);
        ctx.addLifecycleListener(new Tomcat.FixContextListener());
        ctx.setName(name);
        ctx.setPrivileged(true);
        //设置cookie处理策略
        CookieProcessor cookieProcessor = new LegacyCookieProcessor();
        ctx.setCookieProcessor(cookieProcessor);
        //设置非严格校验模式
        tomcat.getConnector().setProperty("allowUnsafeLegacyRenegotiation", "true");
        //跳过扫描
        if (!(skipScan == null || "".equals(skipScan))) {
            StandardJarScanner jarScanner = new StandardJarScanner();
            StandardJarScanFilter jarScanFilter = new StandardJarScanFilter();
            jarScanFilter.setTldSkip(skipScan);
            jarScanFilter.setPluggabilitySkip(skipScan);
            jarScanner.setJarScanFilter(jarScanFilter);
            ctx.setJarScanner(jarScanner);
        }
        //设置启动参数
        tomcat.getConnector().setURIEncoding("UTF-8");
        if (!(jvmStartCommand == null || "".equals(jvmStartCommand))) {
            tomcat.getEngine().setJvmRoute(jvmStartCommand);
        }
        //启动
        tomcat.init();
        tomcat.start();
        tomcat.getServer().await();
    }
}
